const finance = require('../../utils/finance')
Component({
  properties: {
    item: Object,
    isAmountHidden: Boolean,
    deleteFtn: {
      type: String,
      value: 'refresh'
    }
  },
  lifetimes: {
    attached() {
      // console.log('finance-record-card attached:', this.data.item);
      // console.log('finance-record-card properties:', {
      //   item: this.data.item,
      //   isAmountHidden: this.data.isAmountHidden,
      //   deleteFtn: this.data.deleteFtn
      // });
    },
    ready() {
      // console.log('finance-record-card ready:', this.data.item);
    }
  },
  methods: {
    onEdit(e) {
      console.log('onEdit triggered:', e);
      e.stopPropagation && e.stopPropagation();
      const id = this.data.item.id;
      if (!id || id === -1) {
        wx.showToast({ title: '记录ID无效', icon: 'none' });
        return;
      }
      wx.navigateTo({ url: `/pages/finance/write/write?id=${id}` });
    },
    onDelete(e) {
      console.log('onDelete triggered:', e);
      e.stopPropagation && e.stopPropagation();
      const id = this.data.item.id;
      if (!id || id === -1) {
        wx.showToast({ title: '记录ID无效', icon: 'none' });
        return;
      }
      wx.showModal({
        title: '确认删除',
        content: '确定要删除这条记录吗？',
        confirmColor: '#d32f2f',
        success: async (res) => {
          if (res.confirm) {
            wx.showLoading({ title: '删除中...' });
            try {
              await finance.deleteFinanceRecord(id);
              wx.showToast({ title: '删除成功', icon: 'success' });
              this.triggerEvent(this.data.deleteFtn);
            } catch (e) {
              console.error('删除记录失败:', e);
              wx.showToast({ title: (e && e.data && e.data.msg) || '删除失败', icon: 'none' });
            } finally {
              wx.hideLoading();
            }
          }
        }
      });
    },
    // 列表点击事件（加载记账明细）
    async onRecordTap(e) {
      console.log('onRecordTap triggered:', e);
      console.log('onRecordTap target:', e.target);
      console.log('onRecordTap currentTarget:', e.currentTarget);
      const item = this.data.item;
      const id = item.id;
      
      // 检查点击是否在按钮区域
      const target = e.target;
      const className = (target && typeof target.className === 'string') ? target.className : '';
      if (className.includes('record-btn') || className.includes('btn')) {
        console.log('点击的是按钮，不处理卡片点击');
        return; // 如果点击的是按钮，不处理卡片点击
      }
      
      if (!id || id === -1) {
        wx.showToast({ title: '记录ID无效', icon: 'none' });
        return;
      }
      
      // 如果已展开则收起
      if (item.expanded) {
        item.expanded = false;
        this.setData({ item });
        return;
      }
      
      // 请求明细
      wx.showLoading({ title: '加载明细...' });
      try {
        const res = await finance.getFinanceRecordItemList(id);
        if (!Array.isArray(res)){
          console.log('getFinanceRecordItemList接口返回的数据是：' + res + ', 不是数组，不做处理');
          wx.hideLoading();
          return;
        }
        item.details = res || [];
        item.expanded = true;
        this.setData({ item });
      } catch (e) {
        console.error('加载明细失败:', e);
        wx.showToast({ title: (e && e.data && e.data.msg) || '加载明细失败', icon: 'none' });
      } finally {
        wx.hideLoading();
      }
    },
    noop(e) {
      // 阻止事件冒泡，无实际逻辑
      e.stopPropagation && e.stopPropagation();
    }
  }
}); 